package com.example.administrator.demoa;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private static final int SHOW_IMG = 1001;
    private static final int GET_PHONE = 1002;
    private ImageView img;
    private EditText edtKey;
    private TextView txtResult;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        img = findViewById(R.id.iv_img);
        edtKey = findViewById(R.id.edtKey);
        txtResult = findViewById(R.id.txt_result);

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if(msg.what == SHOW_IMG){
                    Bitmap bitmap = (Bitmap) msg.obj;
                    img.setImageBitmap(bitmap);
                    return true;
                }else if(msg.what == GET_PHONE){
                    String html = (String) msg.obj;
                    txtResult.setText(html);
                    return true;
                }
                return false;
            }
        });

        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String phone = edtKey.getText().toString();
                        useHttpURLConnectionGet(phone);
                    }
                }).start();
            }
        });

        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String whether = edtKey.getText().toString();
                        useHttpURLConnectionPost(whether);
                    }
                }).start();
            }
        });
    }

    private void useHttpURLConnectionPost(String whether) {
        String baseUrl = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather";

        try {
            URL url = new URL(baseUrl);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);

            //将路径写入流中
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter =
                    new BufferedWriter(new OutputStreamWriter(outputStream,"utf-8"));
            String body = "theCityCode=".concat(whether).concat("&theUserID=");
            bufferedWriter.write(body);
            bufferedWriter.flush();
            bufferedWriter.close();

            InputStream inputStream = httpURLConnection.getInputStream();
            //读取网页文本
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer buffer = new StringBuffer();
            String line = bufferedReader.readLine();
            while(line != null){
                buffer.append(line);
                line = bufferedReader.readLine();
            }
            Log.e(TAG, "htmlContent = " + buffer);
            //查询天气情况
            Message message = Message.obtain();
            message.what = GET_PHONE;
            message.obj = buffer.toString();
            handler.sendMessage(message);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void useHttpURLConnectionGet(String phone) {
        String src = "http://www.baidu.com";
        String imgUrl = "http://g.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=c8774f6dc91349547e4be060637ebe6e/c8ea15ce36d3d53920a8f82f3887e950342ab041.jpg";
        String baseUrl = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
        String phoneUrl = baseUrl.concat("?mobileCode=").concat(phone).concat("&userID=");
        try {
            URL url = new URL(phoneUrl);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            int code = httpURLConnection.getResponseCode();
            Log.e(TAG, "code = "+ code);

            InputStream inputStream = httpURLConnection.getInputStream();
            //显示图片，通过消息机制
            /*Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            Message message = Message.obtain();
            message.what = SHOW_IMG;
            message.obj = bitmap;
            handler.sendMessage(message);*/

            //读取网页文本
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer buffer = new StringBuffer();
            String line = bufferedReader.readLine();
            while(line != null){
                buffer.append(line);
                line = bufferedReader.readLine();
            }
            Log.e(TAG, "htmlContent = " + buffer);
            //查询手机号归属地
            Message message = Message.obtain();
            message.what = GET_PHONE;
            message.obj = buffer.toString();
            handler.sendMessage(message);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
